package Demo02;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

public class Test {
    public static void main(String[] args) {

    }
}
class MyStack {
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if (queue2.isEmpty()) {
            Queue existQueue = queue1;
            Queue emptyQueue = queue2;
            existQueue.offer(x);
        } else {
            Queue existQueue = queue2;
            Queue emptyQueue = queue1;
            existQueue.offer(x);
        }
    }

    public int pop() {
        Queue existQueue;
        Queue emptyQueue;
        if (queue2.isEmpty()) {
            existQueue = queue1;
            emptyQueue = queue2;
        } else {
            existQueue = queue2;
            emptyQueue = queue1;
        }

        if (existQueue.size() == 0) {
            return -1;
        }
        if (existQueue.size() == 1) {
            int ret = (int) existQueue.peek();
            existQueue.poll();
            return ret;
        }
        int size = existQueue.size();
        while (size > 1) {
            emptyQueue.offer(existQueue.peek());
            existQueue.poll();
            size--;
        }
        int ret = (int) existQueue.peek();
        existQueue.poll();
        return ret;
    }

    public int top() {
        Queue existQueue;
        Queue emptyQueue;
        if(queue2.isEmpty()) {
            existQueue = queue1;
            emptyQueue = queue2;
        } else {
            existQueue = queue2;
            emptyQueue = queue1;
        }

        if(existQueue.size()==0) {
            return -1;
        }
        if(existQueue.size()==1) {
            int ret = (int)existQueue.peek();
            return ret;
        }
        int size = existQueue.size();
        while (size>1) {
            emptyQueue.offer(existQueue.peek());
            existQueue.poll();
            size--;
        }
        int ret = (int)existQueue.poll();
        emptyQueue.offer(ret);
        return ret;
    }

    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.top());
        System.out.println(stack.pop());
        System.out.println(stack.empty());
    }
    public boolean empty() {
/*        if (queue1.isEmpty() && queue2.isEmpty()) {
            return true;
        }
        return false;*/
        return queue1.isEmpty()&&queue2.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */